/**
 * 添加或者修改页面
 */
var AdInfoDlg = {
    data: {
        apId: "",
        title: "",
        pic: "",
        urlType: "",
        url: "",
        status: "",
        sort: "",
        ifDel: "",
        createTime: "",
        updateTime: "",
        deleteTime: ""
    }
};


//单图改变图片
function setAttrPic(index,item){
    //this.attrs[index],'pic',pic);
    console.log('pic',item);
    console.log('index',index);
    $('#demo1').attr('src', item.url); //图片链接（base64）
    $('#'+index).val(item.url); //图片链接（base64）

};

//获取选中的 图片

function changeIMG(index,pic){
    console.log(pic);
    setAttrPic(index,pic);
};

layui.use(['form', 'admin', 'ax','upload','formSelects','layer','func'], function () {
    var $ = layui.jquery;
    var $ax = layui.ax;
    var form = layui.form;
    var admin = layui.admin;
    var upload = layui.upload;
    var formSelects = layui.formSelects;
    var layer = layui.layer;
    var func = layui.func;

    //让当前iframe弹层高度适应
    admin.iframeAuto();

    //初始化所有的试样列表
    formSelects.config('selPosition', {
        searchUrl: Feng.ctxPath + "/adPosition/list",
        searchName: 'positionName',
        keyName: 'positionName',
        keyVal: 'apId'
    });


    //表单提交事件
    form.on('submit(btnSubmit)', function (data) {
        var ajax = new $ax(Feng.ctxPath + "/ad/addItem", function (data) {
            Feng.success("添加成功！");

            //传给上个页面，刷新table用
            admin.putTempData('formOk', true);

            //关掉对话框
            admin.closeThisDialog();

        }, function (data) {
            Feng.error("添加失败！" + data.responseJSON.message)
        });
        ajax.set(data.field);
        ajax.start();

        return false;
    });

    //上传图片
    $('.changeImg').on('click', function(){
        var file_type="";//文件类型
        var inputname="pic";//input 名称
        func.openImg('选择图片',Feng.ctxPath + '/directoryFile/add',"",file_type,inputname);
    });






    //普通图片上传
    var uploadInst = upload.render({
        elem: '#btnPic'
        ,url: '/oss/uploadToDir?directoryId=1&isUser=0'
        ,accept: 'file' //普通文件
        ,before: function(obj){
            //预读本地文件示例，不支持ie8
            obj.preview(function(index, file, result){
                $('#demo1').attr('src', result); //图片链接（base64）
            });
        }
        ,done: function(res){
            //如果上传失败
            if(res.code > 0){
                console.log(JSON.stringify(res));
                document.getElementById('pic').value=res.data;
                return layer.msg('上传成功');
            }else{

            }
            //上传成功
        }
        ,error: function(){
            //演示失败状态，并实现重传
            var demoText = $('#demoText');
            demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
            demoText.find('.demo-reload').on('click', function(){
                uploadInst.upload();
            });
        }
    });

});

